﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Public;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;
using System.Configuration;
using Microsoft.Extensions.Caching.Memory;

namespace BLE_Barcode
{
    class MoseKeyboard
    {
     
        [DllImport("user32")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);

        const uint KEYEVENTF_EXTENDEDKEY = 0x1;
        const uint KEYEVENTF_KEYUP = 0x2;
        [DllImport("user32.dll")]
        static extern short GetKeyState(int nVirtKey);
        [DllImport("user32.dll")]
        static extern void keybd_event(
                          byte bVk,
                          byte bScan,
                          uint dwFlags,
                          uint dwExtraInfo
                                                  );
        public static int x = 0;
        public static int y = 0;

        public static string dbstr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"\\DB\Location.mdb";//Access数据库相对路径;);

        static Microsoft.Extensions.Caching.Memory.MemoryCache cache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions());
        public enum VirtualKeys : byte
        {
            VK_NUMLOCK = 0x90, //数字锁定键
            VK_SCROLL = 0x91,  //滚动锁定
            VK_CAPITAL = 0x14, //大小写锁定
            VK_A = 62,
            VK_LEFT = 37,   //左箭头 
            VK_UP = 38,//上箭头 
            VK_RIGHT = 39,//　右箭头 
            VK_DOWN = 40,//　 下箭头
            VK_RETURN = 13, //回车
            VK_DELETE = Keys.Delete
        }

        private enum MouseOper : byte
        {
            MOVE = 0x0001, /* mouse move */
            LEFTDOWN = 0x0002, /* left button down */
            LEFTUP = 0x0004, /* left button up */
            RIGHTDOWN = 0x0008, /* right button down */
            RIGHTUP = 0x0010,
        }
       

        public static bool GetState(KeyBord Key)
        {
            return (GetKeyState((int)Key) == 1);
        }
        private static void SetState(KeyBord Key, bool State)
        {
            if (State != GetState(Key))
            {
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            }
        }


        public static void LeftButtonClick(int x, int y)//左键单击
        {
            SetCurPos(x, y);
            mouse_event((int)(MouseOper.LEFTDOWN | MouseOper.LEFTUP), 0, 0, 0, 0);
        }
        public static void LeftButtonDoubleClick(int x, int y)//左键双击
        {
            SetCurPos(x, y);
            mouse_event((int)(MouseOper.LEFTDOWN | MouseOper.LEFTUP), 0, 0, 0, 0);
            mouse_event((int)(MouseOper.LEFTDOWN | MouseOper.LEFTUP), 0, 0, 0, 0);
        }
        public static void RightButtonClick(int x, int y)//右键单击
        {
            SetCurPos(x, y);
            mouse_event((int)(MouseOper.RIGHTDOWN | MouseOper.RIGHTUP), 0, 0, 0, 0);
        }
        public static void SetCurPos(int x, int y)
        {
            SetCursorPos(x, y);
        }

        public static void PressKey(KeyBord key)//模拟按键按下和弹起
        {
            keybd_event((byte)key, 0x45, 0, 0);//按键按下
            // Simulate a key release
            keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);//按键弹起
        }
        public static void Compositekey( KeyBord key1, KeyBord key2, KeyBord key3)
        {
            keybd_event((byte)key1, (byte)0, 0, 0);//key1按下     
            keybd_event((byte)key1, (byte)0, 0, 0);//key2按下  
            MoseKeyboard.PressKey(key3);//key3按下弹起
            keybd_event((byte)key2, (byte)0, KEYEVENTF_KEYUP, 0);//key2弹起
            keybd_event((byte)key1, (byte)0, KEYEVENTF_KEYUP, 0);//key1弹起
        }
        public static void Compositekey(KeyBord key1, KeyBord key2)
        {
            keybd_event((byte)key1, (byte)0, 0, 0);//key1按下     
            MoseKeyboard.PressKey(key2);//key3按下弹起
            keybd_event((byte)key1, (byte)0, KEYEVENTF_KEYUP, 0);//key1弹起
        }
        
       
       

        public static bool ExcuteCmd(string cmd)//执行单行命令
        {
            if (cmd == "")
                return false;
            //cmd格式是：延时,类型,(鼠标X,Y)，命令
            string[] str = cmd.Split(',');//分解命令
            if(str.Length < 3 || str.Length >5)//命令是否正确
                return false;
            try
            {
                Thread.Sleep(int.Parse(str[0]));//延时操作
                if (str[1] == "KeyBord")//键盘操作
                {
                    string[] keys = str[2].Split('+');//分解按键
                    if (keys.Length == 1)//单个健按下
                    {

                        //string cm = "{code:1}";//keys[0]
                        if (keys[0].Contains("BARCODE"))
                        {
                            //char[] data = get_barcodeForAccess(keys[0]);

                            //var cache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions());
                           
                           
                            var cache = MemoryCacheSingleton.Instance;
                            
                            string sn = cache.Get("getDeviceAddress")+"";
                            //string s = ConfigurationManager.AppSettings["GetDeviceAddress"];
                            //new char[] { 'a','B' };// 
                            char[] data = sn.ToCharArray();  
                            for (int i = 0; i < data.Length; i++)
                            {
                                Thread.Sleep(int.Parse(str[0]));//延时操作
                                MoseKeyboard.PressKey(GetKeyByString(data[i].ToString().ToUpper()));
                            }
                        }
                        else
                        {
                            MoseKeyboard.PressKey(GetKeyByString(keys[0]));
                        }
                     
                      
                        
                        return true;
                    }
                    if (keys.Length == 2)//2个复合键按下
                    {
                        MoseKeyboard.Compositekey(GetKeyByString(keys[0]),GetKeyByString( keys[1]));
                        return true;
                    }
                    if (keys.Length == 2)//3个复合键按下
                    {
                        MoseKeyboard.Compositekey(GetKeyByString(keys[0]), GetKeyByString(keys[1]),GetKeyByString(keys[2]));
                        return true;
                    }
                    return false;
                }
                if (str[1] == "Mouse")//鼠标操作
                {
                    if (str[4] == "LeftButtonClick")//左键单击
                    {
                        MoseKeyboard.LeftButtonClick(int.Parse(str[2]), int.Parse(str[3]));
                        return true;
                    }
                    if (str[4] == "LeftButtonDoubleClick")//左键双击
                    {
                        MoseKeyboard.LeftButtonDoubleClick(int.Parse(str[2]), int.Parse(str[3]));
                        return true;
                    }
                    if (str[4] == "RightButtonClick")//右键单击
                    {
                        MoseKeyboard.RightButtonClick(int.Parse(str[2]), int.Parse(str[3]));
                        return true;
                    }
                    return false;
                }
                    return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static KeyBord GetKeyByString(string key)
        {
            WriteLogTools.WriteMessage("GetKeyByString：" + key);
            if (key == "A")
                return KeyBord.A;
            if (key == "B")
                return KeyBord.B;
            if (key == "C")
                return KeyBord.C;
            if (key == "D")
                return KeyBord.D;
            if (key == "E")
                return KeyBord.E;
            if (key == "F")
                return KeyBord.F;
            if (key == "G")
                return KeyBord.G;
            if (key == "H")
                return KeyBord.H;
            if (key == "I")
                return KeyBord.I;
            if (key == "J")
                return KeyBord.J;
            if (key == "K")
                return KeyBord.K;
            if (key == "L")
                return KeyBord.L;
            if (key == "M")
                return KeyBord.M;
            if (key == "N")
                return KeyBord.N;
            if (key == "O")
                return KeyBord.O;
            if (key == "P")
                return KeyBord.P;
            if (key == "Q")
                return KeyBord.Q;
            if (key == "R")
                return KeyBord.R;
            if (key == "S")
                return KeyBord.S;
            if (key == "T")
                return KeyBord.T;
            if (key == "U")
                return KeyBord.U;
            if (key == "V")
                return KeyBord.V;
            if (key == "W")
                return KeyBord.W;
            if (key == "X")
                return KeyBord.X;
            if (key == "Y")
                return KeyBord.Y;
            if (key == "Z")
                return KeyBord.Z;//A-Z
            if (key == "F1")
                return KeyBord.F1;
            if (key == "F2")
                return KeyBord.F2;
            if (key == "F3")
                return KeyBord.F3;
            if (key == "F4")
                return KeyBord.F4;
            if (key == "F5")
                return KeyBord.F5;
            if (key == "F6")
                return KeyBord.F6;
            if (key == "F7")
                return KeyBord.F7;
            if (key == "F8")
                return KeyBord.F8;
            if (key == "F9")
                return KeyBord.F9;
            if (key == "F10")
                return KeyBord.F10;
            if (key == "F11")
                return KeyBord.F11;
            if (key == "F12")
                return KeyBord.F12;//F1-F12
            if (key == "Esc")
                return KeyBord.Escape;
            if (key == "0")
                return KeyBord.D0;
            if (key == "1")
                return KeyBord.D1;
            if (key == "2")
                return KeyBord.D2;
            if (key == "3")
                return KeyBord.D3;
            if (key == "4")
                return KeyBord.D4;
            if (key == "5")
                return KeyBord.D5;
            if (key == "6")
                return KeyBord.D6;
            if (key == "7")
                return KeyBord.D7;
            if (key == "8")
                return KeyBord.D8;
            if (key == "9")
                return KeyBord.D9;//0-9
            if (key == "-")
                return KeyBord.OemMinus;
            if (key == "=")
                return KeyBord.Oemplus;
            if (key == @"\")
                return KeyBord.OemBackslash;
            if (key == "BackSpace")
                return KeyBord.Backspace;
            if (key == "Tab")
                return KeyBord.Tab;
            if (key == "CapsLock")
                return KeyBord.CapsLock;
            if (key == "Windows")
                return KeyBord.LWin;
            if (key == "Alt")
                return KeyBord.Alt;
            if (key == "Shift")
                return KeyBord.ShiftKey;
            if (key == "Control")
                return KeyBord.ControlKey;
            if (key == "[")
                return KeyBord.OemOpenBrackets;
            if (key == "]")
                return KeyBord.Oem6;
            if (key == ":")
                return KeyBord.OemSemicolon;
            if (key == ";")
                return KeyBord.OemSemicolon;
            if (key == "'")
                return KeyBord.OemQuotes;
            if (key == ",")
                return KeyBord.Oemcomma;
            if (key == ".")
                return KeyBord.OemPeriod;
            if (key == "?")
                return KeyBord.OemQuestion;
            if (key == "Space")
                return KeyBord.Space;
            if (key == "<")
                return KeyBord.Oemcomma;
            if (key == ">")
                return KeyBord.OemPeriod;
            if (key == "/")
                return KeyBord.OemQuestion;
            if (key == "~")
                return KeyBord.Oem3;
            if (key == "Inst")/////////////////控制键盘
                return KeyBord.Insert;
            if (key == "Home")
                return KeyBord.Home;
            if (key == "End")
                return KeyBord.End;
            if (key == "Enter")
                return KeyBord.Enter;
            if (key == "PageDw")
                return KeyBord.PageDown;
            if (key == "PageUp")
                return KeyBord.PageUp;
            if (key == "Left")
                return KeyBord.Left;
            if (key == "Right")
                return KeyBord.Right;
            if (key == "Up")
                return KeyBord.Up;
            if (key == "Down")
                return KeyBord.Down;
            if (key == "PrintScreen")
                return KeyBord.PrintScreen;
            if (key == "Scroll")
                return KeyBord.Scroll;
            if (key == "Num0")//数字键盘
                return KeyBord.NumPad0;
            if (key == "Num1")
                return KeyBord.NumPad1;
            if (key == "Num2")
                return KeyBord.NumPad2;
            if (key == "Num3")
                return KeyBord.NumPad3;
            if (key == "Num4")
                return KeyBord.NumPad4;
            if (key == "Num5")
                return KeyBord.NumPad5;
            if (key == "Num6")
                return KeyBord.NumPad6;
            if (key == "Num7")
                return KeyBord.NumPad7;
            if (key == "Num8")
                return KeyBord.NumPad8;
            if (key == "Num9")
                return KeyBord.NumPad9;
            if (key == "NumAdd")
                return KeyBord.Add;
            if (key == "Num/")
                return KeyBord.Divide;
            if (key == "Num*")
                return KeyBord.Multiply;
            if (key == "Num-")
                return KeyBord.Subtract;
            if (key == "NumEnter")
                return KeyBord.Enter;
            if (key == "Num.")
                return KeyBord.OemPeriod;
            if (key == "NumLock")
                return KeyBord.NumLock;


            return KeyBord.T;
        }

    }
}